#include "page_pop.h"
#include "R.h"
#include "conf_mgr.h"
#include "wind_mgr.h"

PagePop::PagePop(ViewGroup *wind_pop_box, int type, pop_callback enterCb, pop_callback cancelCb, int position)
    : mWindPopBox(wind_pop_box), mType(type), mEnterCallback(enterCb), mCancelCallback(cancelCb), mPosition(position)
{
    mPopLayout = (ViewGroup *)LayoutInflater::from(mWindPopBox->getContext())->inflate("@layout/mode_delete_fix", nullptr);
    mWindPopBox->addView(mPopLayout);

    ModeData modeData;
    modeData = d_fileLoad->getModeDataList()[position];

    mPopModeName = (TextView *)mPopLayout->findViewById(demo::R::id::name);
    mPopModeTemp = (TextView *)mPopLayout->findViewById(demo::R::id::temp_num);
    mPopModeChuShui = (TextView *)mPopLayout->findViewById(demo::R::id::chu_shui_liang_num);
    mPopSubmit = (TextView *)mPopLayout->findViewById(demo::R::id::mode_delete_fix_sure);
    mPopCancle = (TextView *)mPopLayout->findViewById(demo::R::id::mode_delete_fix_quxiao);

    mPopModeName->setText(modeData.modeName);
    mPopModeTemp->setText(modeData.temperature);
    mPopModeChuShui->setText(std::to_string(modeData.chuShuiLiang));

    mPopLayout->setOnClickListener(std::bind(&PagePop::onBtnClickListener, this, std::placeholders::_1)); // 防止点击穿透
    mPopSubmit->setOnClickListener(std::bind(&PagePop::onBtnClickListener, this, std::placeholders::_1));
    mPopCancle->setOnClickListener(std::bind(&PagePop::onBtnClickListener, this, std::placeholders::_1));
}

void PagePop::onBtnClickListener(View &v)
{
    switch (v.getId())
    {
    case demo::R::id::mode_delete_fix_sure:
    {
        if (mEnterCallback)
        {
            mEnterCallback();
        }
        d_windMgr->dealClosePopPage();
        break;
    }
    case demo::R::id::mode_delete_fix_quxiao:
    {
        if (mCancelCallback)
        {
            mCancelCallback();
        }
        d_windMgr->dealClosePopPage();
        break;
    }
    default:
        break;
    }

}

PagePop::~PagePop()
{
    mWindPopBox->removeView(mPopLayout);
    delete mPopLayout;
}